<template>
  <view>
    <view v-if="showFlag" class="container-home">
      <view class="tabs" v-if="tabList.length===2">
        <uni-segmented-control :current="currentTab" :values="tabList" style-type="button" :active-color="'#409EFE'"
          @clickItem="tabChange" />
      </view>
      <view class="title-box" @click="showPopClick"
        :style="{'width': screenWidth + 'px','height': screenHeight + 'px',}">
        <image :src="bacImg" class="back-img"></image>
        <view class="title-items">
          <view class="til font-15">在途车辆</view>
          <view class="val font-24 font-bold">{{carsDatas.numberOfVehiclesInTransit || 0}}</view>
        </view>
        <view class="line"></view>
        <view class="items">
          <view v-if="curTab==='采购'">
            <view class="til font-13">停车场车辆</view>
            <view class="val font-20">{{carsDatas.numberOfVehiclesInTheParkingLot || 0}}</view>
          </view>
          <view>
            <view class="til font-13">场内车辆</view>
            <view class="val font-20">{{carsDatas.numberOfVehiclesOnSite || 0}}</view>
          </view>
          <view>
            <view class="til font-13">已出场车辆</view>
            <view class="val font-20">{{carsDatas.numberOfVehiclesExited || 0}}</view>
          </view>
        </view>
      </view>
      <view class="goods-list">
        <view class="item" style="margin-bottom: 10px;">
          <cars-statics :itemList="carsDatas.numberOfVehiclesInTransits" :curTab="curTab" title="在途车辆数"
            :orgShow="orgShow" types="1"></cars-statics>
          <cars-statics v-if="curTab==='采购'" :itemList="carsDatas.numberOfVehiclesInTheParkingLots" :curTab="curTab"
            :orgShow="orgShow" title="停车场车辆数" types="2"></cars-statics>
          <cars-statics :itemList="carsDatas.numberOfVehiclesOnSites" :curTab="curTab" title="场内车辆数" :orgShow="orgShow"
            types="3"></cars-statics>
          <cars-statics :itemList="carsDatas.numberOfVehiclesExiteds" :curTab="curTab" title="已出场车辆数" :orgShow="orgShow"
            types="4"></cars-statics>
        </view>
      </view>
    </view>
    <view v-else class="noauth-box">
      {{messAuth}}
    </view>
    <!-- <custom-tabbar class="tabbar-box" :list="tabbarLists"></custom-tabbar> -->
    <view class="tabbar-box">
      <view v-for="(item,index) in tabbarLists" :key="index" class="my-tabbar"
        v-if="item.pagePath == '/pages/plans/index' ? (hasMenuPer && showPlanMenu) : !item.hidden">
        <view hover-class="none" class="weui-flex__item menu-item" @click="goToTab(item.pagePath)"
          :style="item.active? 'color: '+(item.selectedColor? item.selectedColor : selectedColor) : ''">
          <image :src="item.selectedIconPath" v-if="item.active" class="img"></image>
          <image :src="item.iconPath" v-if="!item.active" class="img"></image>
          <text class='tabbar_text'>{{item.text}}</text>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
  import { getCarsLists, getSellCount, getTenantConfig } from "@/api/me.js"
  import config from '@/config'
  import { getInfo } from '@/api/login';
  import { getBasisDeploy, getPurModel } from "@/api/system/user.js"
  import { getAccountIdentity, getDriverId, getTenantId, setPurchaseModel, getPurchaseModel } from '@/utils/auth.js';
  import { toast, getButtonShow } from '@/utils/common.js';
  export default {
    data() {
      return {
        showPlanMenu: false,
        showFlag: true,
        orgShow: false,
        donotShow: false,
        messAuth: '',
        carsDatas: {},
        screenWidth: 0,
        screenHeight: 0,
        tabList: [],
        currentTab: 0,
        tabbarLists: [
          {
            "pagePath": "/pages/index",
            "hidden": false,
            "active": true,
            "selectedColor": '#409EFE',
            "iconPath": require("static/images/tabbar/index.png"),
            "selectedIconPath": require("static/images/tabbar/index_.png"),
            "text": "首页"
          },
          {
            "pagePath": "/pages/indexs",
            "hidden": false,
            "active": false,
            "selectedColor": '#409EFE',
            "iconPath": require("static/images/tabbar/home.png"),
            "selectedIconPath": require("static/images/tabbar/home_.png"),
            "text": "排号"
          },
          {
            "pagePath": "/pages/plans/index",
            "hidden": false,
            "active": false,
            "selectedColor": '#409EFE',
            "iconPath": require("static/images/tabbar/plan.png"),
            "selectedIconPath": require("static/images/tabbar/plan_.png"),
            "text": "计划"
          },
          {
            "pagePath": "/pages/waybill/index",
            "hidden": false,
            "active": false,
            "selectedColor": '#409EFE',
            "iconPath": require("static/images/tabbar/work.png"),
            "selectedIconPath": require("static/images/tabbar/work_.png"),
            "text": "运单"
          },
          {
            "pagePath": "/pages/mine/index",
            "hidden": false,
            "active": false,
            "selectedColor": '#409EFE',
            "iconPath": require("static/images/tabbar/mine.png"),
            "selectedIconPath": require("static/images/tabbar/mine_.png"),
            "text": "我的"
          }
        ],
        hasMenuPer: true,
      }
    },
    computed: {
      bacImg() {
        if (this.$store.state.user.curTypeColor == "#D22C2C") {
          return config.baseImgUrl + '/bac1.png'
        } else if (this.$store.state.user.curTypeColor == "#409EFE") {
          return config.baseImgUrl + '/bac.png'
        } else {
          return config.baseImgUrl + '/bac.png'
        }
      },
      curTab() {
        return this.tabList[this.currentTab]
      }
    },
    methods: {
      goToTab(url) {
        wx.switchTab({
          url: url  // 需要跳转的 tab 页路径
        })
      },
      tabChange(e) {
        this.currentTab = e.currentIndex
        if (this.tabList[this.currentTab] === '销售') {
          this.getSellCount()
        }
        if (this.tabList[this.currentTab] === '采购') {
          this.getDatas()
        }
      },
      async getTenantConfig() {
        let res = await getTenantConfig();
        if (res.code === 0) {
          this.tabList = []
          const result = res.data.bizType || []
          if (result.includes('2')) {
            this.tabList.push('销售')
            this.getSellCount()
          }
          if (result.includes('1')) {
            this.tabList.push('采购')
            if (this.tabList.length === 1) {
              this.getDatas()
            }
          }
        }
      },
      async getSellCount() {
        const res = await getSellCount()
        this.carsDatas = {}
        if (res.code === 0) {
          this.carsDatas = res.data
        }
        //停止下拉刷新
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
      },
      async getDatas() {
        const res = await getCarsLists()
        this.carsDatas = {}
        if (res.code == "0") {
          this.carsDatas = res.data
        }
        //停止下拉刷新
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
      },
      async getbuttonAuth() {
        //调用公共方法
        let indexShow = 'enterApp:index'
        let flag = await getButtonShow(indexShow)
        if (!flag) {
          this.messAuth = '您无权查看此页面！请联系管理员'
          this.showFlag = false
        } else {
          this.showFlag = true
        }
      },
    },
    async onShow() {
      let purmodel = await getPurModel()
      if (purmodel.code == 0) {
        this.showPlanMenu = purmodel.data.purchaseBizMode == 1 ? true : false
        setPurchaseModel(purmodel.data.purchaseBizMode)
      }
      let permissionsLists = [];
      let plists = await getInfo({
        enterId: getTenantId(),
        userId: getDriverId()
      });
      permissionsLists = plists?.data?.permissions || [];
      this.hasMenuPer = permissionsLists.some(
        (item) => item == 'plan:view'
      );
      let res = await getBasisDeploy(getTenantId())
      if (res.code == 200) {
        this.orgShow = res?.data?.isEnabledOrganization === 1 ? true : false
      }
      if (getAccountIdentity() === 0 || getAccountIdentity() === 1 || getAccountIdentity() === 2) {
        if (getAccountIdentity() === 1 || getAccountIdentity() === 2) {
          const systemInfo = uni.getSystemInfoSync();
          // 从系统信息中提取屏幕宽度
          this.currentTab = 0
          this.screenWidth = systemInfo.screenWidth;
          this.screenHeight = systemInfo.screenWidth / 2.3;
          this.getbuttonAuth()
          this.getTenantConfig()
        } else {
          uni.reLaunch({
            url: '/pages/devices/working'
          });
        }
      } else {
        uni.reLaunch({
          url: '/pages/login'
        });
      }
    },
    onPullDownRefresh() {
      this.getbuttonAuth()
      if (this.tabList[this.currentTab] === '销售') {
        this.getSellCount()
      }
      if (this.tabList[this.currentTab] === '采购') {
        this.getDatas()
      }
    }
  };
</script>

<style lang="scss" scoped>
  .tabs {
    padding: 0 10px 10px 10px;
    background-color: #ffffff;
  }
</style>